home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / findfile.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  6KB  |  265 lines

  1.  
  2.  
  3. /*  Copyright (c) 1993-1996 Algorithms Corporation  */
  4. /*  All rights reserved.  */
  5.  
  6.  
  7.  
  8.  
  9. /*  This file automatically generated by dpp - do not edit  */
  10.  
  11. #define    DPP_STRATEGY    2
  12. #define    DPP_FASTWIDE    0
  13.  
  14.  
  15.  
  16. #line 15 "findfile.d"
  17. #if defined(_WIN32) || defined(WIN32) 
  18. #include <io.h> 
  19. #define FINDSTRUCT _finddata_t 
  20. #ifndef _WIN32 
  21. #define _WIN32 
  22. #endif 
  23. #else 
  24. #include <dos.h> 
  25. #include <sys/types.h> 
  26. #include <sys/stat.h> 
  27. #define FINDSTRUCT find_t 
  28. #endif 
  29.  
  30.  
  31. #include "findfile.h" 
  32.  
  33. #define    CLASS    FindFile_c
  34. #define    ivType    FindFile_iv_t
  35.  
  36. #include "generics.h"
  37.  
  38. object    FindFile_c;
  39.  
  40.  
  41. #line 42 "findfile.c"
  42. typedef struct  _FindFile_iv_t  {
  43.     object iFileName;
  44.     unsigned iAttributes;
  45.     struct FINDSTRUCT iData;
  46.     long iHandle;
  47.     int iFirst;
  48.     int iEnd;
  49. }    FindFile_iv_t;
  50.  
  51.  
  52.  
  53. #line 42 "findfile.d"
  54. cmeth objrtn FindFile_cm_gNewFindFile(object self, char *name, int attr)
  55.     object obj = oSuper(FindFile_c, gNew, self)(self); 
  56.     ivType *iv = ivPtr(obj); 
  57.     iv->iFileName = gNewWithStr(String, name); 
  58.     iv->iAttributes = attr; 
  59.     iv->iHandle = -1L; 
  60.     return obj; 
  61.  
  62. imeth objrtn FindFile_im_gDispose(object self)
  63. { FindFile_iv_t *iv = GetIVs(FindFile, self);
  64. #ifdef _WIN32 
  65.         if (iv->iHandle != -1L) 
  66.         _findclose(iv->iHandle); 
  67. #endif 
  68.         gDispose(iv->iFileName); 
  69.     return oSuper(FindFile_c, gDispose, self)(self); 
  70.  
  71. imeth objrtn FindFile_im_gGCDispose(object self)
  72. { FindFile_iv_t *iv = GetIVs(FindFile, self);
  73. #ifdef _WIN32 
  74.         if (iv->iHandle != -1L) 
  75.         _findclose(iv->iHandle); 
  76. #else 
  77.         USE(iv->iHandle); 
  78. #endif 
  79.         return oSuper(FindFile_c, gDispose, self)(self); 
  80.  
  81. #define SET(a, b) ((a) & (b)) 
  82.  
  83. static int valid(ivType *iv) 
  84.     if (SET(iv->iData.attrib, _A_SUBDIR)) { 
  85.         if (!SET(iv->iAttributes, FF_DIRECTORY)) 
  86.             return 0; 
  87.     } else 
  88.         if (!SET(iv->iAttributes, FF_FILE)) 
  89.         return 0; 
  90.     if (SET(iv->iData.attrib, _A_RDONLY)) { 
  91.         if (!SET(iv->iAttributes, FF_READONLY)) 
  92.             return 0; 
  93.     } else 
  94.         if (!SET(iv->iAttributes, FF_READWRITE)) 
  95.         return 0; 
  96.     if (SET(iv->iData.attrib, _A_HIDDEN) && !SET(iv->iAttributes, FF_HIDDEN)) 
  97.         return 0; 
  98.     if (SET(iv->iData.attrib, _A_SYSTEM) && !SET(iv->iAttributes, FF_SYSTEM)) 
  99.         return 0; 
  100.     if (SET(iv->iAttributes, FF_ARCHIVE_ONLY) && !SET(iv->iData.attrib, _A_ARCH)) 
  101.         return 0; 
  102.     return 1; 
  103.  
  104. imeth char * FindFile_im_gNextFile(object self)
  105. { FindFile_iv_t *iv = GetIVs(FindFile, self);
  106. #ifdef _WIN32 
  107.         int r; 
  108.  
  109.     if (iv->iEnd) 
  110.         return NULL; 
  111.     if (!iv->iFirst) { 
  112.         iv->iHandle = _findfirst(gStringValue(iv->iFileName), &iv->iData); 
  113.         iv->iFirst = 1; 
  114.         r = iv->iHandle < 0L ? 1 : 0; 
  115.     } else 
  116.         r = _findnext(iv->iHandle, &iv->iData); 
  117.     while (!r && !valid(iv)) 
  118.         r = _findnext(iv->iHandle, &iv->iData); 
  119.     if (r) { 
  120.         iv->iEnd = 1; 
  121.         if (iv->iHandle != -1L) { 
  122.             _findclose(iv->iHandle); 
  123.             iv->iHandle = -1; 
  124.         } 
  125.         return NULL; 
  126.     } else 
  127.         return iv->iData.name; 
  128. #else 
  129.         unsigned r; 
  130.  
  131.     if (iv->iEnd) 
  132.         return NULL; 
  133.     if (!iv->iFirst) { 
  134.         r = _dos_findfirst(gStringValue(iv->iFileName), 
  135.             _A_ARCH | _A_HIDDEN | _A_NORMAL | _A_RDONLY | _A_SUBDIR | _A_SYSTEM, 
  136.             &iv->iData); 
  137.         iv->iFirst = 1; 
  138.     } else 
  139.         r = _dos_findnext(&iv->iData); 
  140.     while (!r && !valid(iv)) 
  141.         r = _dos_findnext(&iv->iData); 
  142.     if (r) { 
  143.         iv->iEnd = 1; 
  144.         return NULL; 
  145.     } else 
  146.         return iv->iData.name; 
  147. #endif 
  148.     } 
  149.  
  150. imeth long FindFile_im_gLength(object self)
  151. { FindFile_iv_t *iv = GetIVs(FindFile, self);
  152.     return iv->iFirst && !iv->iEnd ? iv->iData.size : -1L; 
  153.  
  154. imeth char * FindFile_im_gName(object self)
  155. { FindFile_iv_t *iv = GetIVs(FindFile, self);
  156.     return iv->iFirst && !iv->iEnd ? iv->iData.name : NULL; 
  157.  
  158. #ifndef _WIN32 
  159. static void getpath(char *path, char *search, char *name) 
  160.     char *p, *last; 
  161.  
  162.     strcpy(path, search); 
  163.     for (last=path-1, p=path ; *p ; ++p) 
  164.         if (*p == ':' || *p == '/' || *p == '\\') 
  165.         last = p; 
  166.     strcpy(last+1, name); 
  167. #endif 
  168.  
  169. imeth long FindFile_im_gWriteTime(object self)
  170. { FindFile_iv_t *iv = GetIVs(FindFile, self);
  171. #ifdef _WIN32 
  172.         if (!iv->iFirst || iv->iEnd) 
  173.         return -1L; 
  174.     return iv->iData.time_write; 
  175. #else 
  176.         char path[256]; 
  177. #ifdef MSC16 
  178.         struct _stat sb; 
  179. #else 
  180.         struct stat sb; 
  181. #endif 
  182.  
  183.         if (!iv->iFirst || iv->iEnd) 
  184.         return -1L; 
  185.     getpath(path, gStringValue(iv->iFileName), iv->iData.name); 
  186.     return _stat(path, &sb) ? -1L : sb.st_mtime; 
  187. #endif 
  188.     } 
  189.  
  190. imeth unsigned FindFile_im_gAttributes(object self)
  191. { FindFile_iv_t *iv = GetIVs(FindFile, self);
  192.     unsigned at = 0; 
  193.  
  194.     if (!iv->iFirst || iv->iEnd) 
  195.         return at; 
  196.     if (SET(iv->iData.attrib, _A_SUBDIR)) 
  197.         at |= FF_DIRECTORY; 
  198.     else 
  199.         at |= FF_FILE; 
  200.     if (SET(iv->iData.attrib, _A_RDONLY)) 
  201.         at |= FF_READONLY; 
  202.     else 
  203.         at |= FF_READWRITE; 
  204.     if (SET(iv->iData.attrib, _A_HIDDEN)) 
  205.         at |= FF_HIDDEN; 
  206.     if (SET(iv->iData.attrib, _A_SYSTEM)) 
  207.         at |= FF_SYSTEM; 
  208.     if (SET(iv->iData.attrib, _A_ARCH)) 
  209.         at |= FF_ARCHIVE_ONLY; 
  210.     return at; 
  211.  
  212.  
  213. #line 225 "findfile.c"
  214.  
  215. objrtn    FindFile_initialize(void)
  216. {
  217.     static  CRITICALSECTION  cs;
  218.     static  int volatile once = 0;
  219.  
  220.     ENTERCRITICALSECTION(_CI_CS_);
  221.     if (!once) {
  222.         INITIALIZECRITICALSECTION(cs);
  223.         once = 1;
  224.     }
  225.     LEAVECRITICALSECTION(_CI_CS_);
  226.  
  227.     ENTERCRITICALSECTION(cs);
  228.  
  229.     if (FindFile_c) {
  230.         LEAVECRITICALSECTION(cs);
  231.         return FindFile_c;
  232.     }
  233.     INHIBIT_THREADER;
  234.     FindFile_c = gNewClass(Class, "FindFile", sizeof(FindFile_iv_t), 0, END);
  235.     cMethodFor(FindFile, gNewFindFile, FindFile_cm_gNewFindFile);
  236.     iMethodFor(FindFile, gNextFile, FindFile_im_gNextFile);
  237.     iMethodFor(FindFile, gLength, FindFile_im_gLength);
  238.     iMethodFor(FindFile, gAttributes, FindFile_im_gAttributes);
  239.     iMethodFor(FindFile, gWriteTime, FindFile_im_gWriteTime);
  240.     iMethodFor(FindFile, gDispose, FindFile_im_gDispose);
  241.     iMethodFor(FindFile, gName, FindFile_im_gName);
  242.     iMethodFor(FindFile, gGCDispose, FindFile_im_gGCDispose);
  243.     iMethodFor(FindFile, gDeepDispose, FindFile_im_gDispose);
  244.  
  245.     ENABLE_THREADER;
  246.  
  247.     LEAVECRITICALSECTION(cs);
  248.  
  249.     return FindFile_c;
  250. }
  251.  
  252.  
  253.  
  254.